package websocket;

import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.java_websocket.WebSocket;
import org.java_websocket.WebSocketImpl;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.framing.FrameBuilder;
import org.java_websocket.framing.Framedata;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;


/**
 * websocket测试用服务器
 * @author huanghaiyun
 * @createTime 2016-1-15上午10:56:10
 */
public class WebsocketServerTest extends WebSocketServer {
	
	private static final WebsocketServerTest INSTANCE=new WebsocketServerTest(new InetSocketAddress(7897), 10,new Draft_17());
	
	private static int counter = 0;
	private static List<WebSocket> list=new ArrayList<WebSocket>();//所有的连接
	
	private boolean inited = false;
	private static final Logger LOG = Logger.getLogger(WebsocketServerTest.class);//日志
	
	/**
	 * 初始化类
	 * @param address 地址
	 * @param d 链接协议
	 */
	private WebsocketServerTest( InetSocketAddress address,int decodercount, Draft d ) {
		super( address, decodercount,Collections.singletonList( d ) );
	}
	
	/**
	 * 获得单例的 websocket ,并将其启动
	 * @return 返回单例的 websocket 
	 */
	public static WebsocketServerTest getInstance(){
		return INSTANCE;
	}
	
	/**
	 * 开启服务: (WebSocketService.init)
	 *
	 */
	public void init(){
		if(inited){
			LOG.warn("the websocket service is running!!!");
		}else{
			super.start();
		}
		LOG.info("websocket服务已开启成功。");
	}
	

	public List<WebSocket> getList() {
		return list;
	}

	public void setList(List<WebSocket> list) {
		this.list = list;
	}

	public WebsocketServerTest( int port , Draft d ) throws UnknownHostException {
		super( new InetSocketAddress( port ), Collections.singletonList( d ) );
	}
	
	public WebsocketServerTest( InetSocketAddress address, Draft d ) {
		super( address, Collections.singletonList( d ) );
	}

	@Override
	public void onOpen( WebSocket conn, ClientHandshake handshake ) {
		counter++;
		list.add(conn);
		System.out.println( "///////////Opened connection number" + counter );
		conn.send("hello");
	}

	@Override
	public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
		list.remove(conn);
		System.out.println( "closed" );
	}

	@Override
	public void onError( WebSocket conn, Exception ex ) {
		System.out.println( "Error:" );
		ex.printStackTrace();
	}

	@Override
	public void onMessage( WebSocket conn, String message ) {
		System.out.println("websocketService get message:"+message);
	}

	@Override
	public void onMessage( WebSocket conn, ByteBuffer blob ) {
		conn.send( blob );
	}

	public static void main( String[] args ) throws  UnknownHostException {
		WebSocketImpl.DEBUG = false;
		int port;
		try {
			port = new Integer( args[ 0 ] );
		} catch ( Exception e ) {
			System.out.println( "No port specified. Defaulting to 9001" );
			port = 9001;
		}
		new WebsocketServerTest( port, new Draft_17() ).start();
	} 

}
